package cn.chenmanman.system.controller;

import cn.chenmanman.common.result.Result;
import cn.chenmanman.model.system.SysRole;
import cn.chenmanman.model.vo.AssginRoleVo;
import cn.chenmanman.model.vo.SysRoleQueryVo;
import cn.chenmanman.system.exception.extend.BizException;
import cn.chenmanman.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询所有角色
     * */
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("findAll")
    public Result findAllRole(){
        List<SysRole> list = sysRoleService.list(null);
        try {
            System.out.println(9 / 0);
        }catch (ArithmeticException e){
            throw new BizException(400, "这是我自定义的异常");
        }
        return Result.ok(list);
    }


    /**
     * 删除角色接口
     * */
    @ApiOperation(value = "删除指定角色")
    @DeleteMapping("remove/{id}")
    public Result removeRole(@PathVariable("id") Long id){
        boolean isSuccess = sysRoleService.removeById(id);
        if (!isSuccess){
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value="条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result selectPage(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit,
                             SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> roleModel = sysRoleService.selectPage(pageParam, sysRoleQueryVo);
        return Result.ok(roleModel);
    }

    @ApiOperation(value = "添加新角色")
    @PostMapping
    public Result saveRole(@RequestBody SysRole sysRole){
        return sysRoleService.save(sysRole) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "获取指定角色")
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Long id) {
        return Result.ok(sysRoleService.getById(id));
    }

    @ApiOperation(value="更新角色")
    @PostMapping("update")
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.updateById(sysRole);
        return b ? Result.ok():Result.fail();
    }

    @ApiOperation( value = "批量删除角色" )
    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids){
        boolean b = sysRoleService.removeByIds(ids);
        return b?Result.ok():Result.fail();
    }


    // 获取用户所拥有的角色
    @ApiOperation("获取用户所有有的角色")
    @GetMapping("userRoles")
    public Result getUserRoles(String userId) {
        Map<String, Object> roles = sysRoleService.getUserRoles(userId);
        return Result.ok(roles);
    }

    @ApiOperation("为用户分配角色")
    @PostMapping("assginRoles")
    public Result assginRoles(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.assginRoles(assginRoleVo);
        return Result.ok();
    }
}
